import axios from "axios";
import qs from "qs";
import { my } from "@/utils/my.js";
// import {sign} from "@/utils/sign.js";
import { Toast } from "vant";

export var request = async (type = "POST", data = {}, fn = null) => {

  for ( var i in data) {
      if(typeof(data[i])=="undefined" || data[i]==null || data[i]=='undefined'){
        data[i] = '';
      }
  }

  if (data.is_loading == true) {
    Toast({
      type: "loading",
      duration: 20000,
      message: "加载中..."
    });
  }
  let result;
  type = type.toUpperCase();
  let url = window.my.api_url+Math.random();
  data["partner"] = "5300";
  if (type === "GET") {
    await axios.get(url, { params: data }).then(res => {
      result = res.data;
      if (data.is_loading == true) {
        Toast.clear();
      }
      if (fn) {
        fn();
      }
    });
  } else if (type === "POST") {
    await axios.post(url, qs.stringify(data)).then(res => {
      result = res.data;
      if (data.is_loading == true) {
        Toast.clear();
      }
      if (fn) {
        fn();
      }
    });
  }
  if (result.code == 200) {
    //my.removeLocal("token");
    //my.removeLocal("userinfo");
    //window.location.href = '/login'
  }
  return result;
};
